//获取二维码
exports.temporaryQrCode = function(req, res) {

	//获取restful参数
	var bodoyParams = req.routeInfo.bodyParams;

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;
    
    var newsParams = restfulParams.sceneStr + '|&bpau_uid='+restfulParams.bpau_uid + '&oa_id='+restfulParams.oa_id + '&bpau_member_count='+restfulParams.bpau_member_count;
    //console.log('--------啦啦啦啦------------');
    //console.log(restfulParams);
    //console.log(newsParams);
    //console.log('-------------啦啦啦啦-------');
    
	//最终反回结果
	var resultData = {};

	//保存access_token
	var accessToken = "";

	//保存jsapi_ticket
	var jsapiTicket = "";

	//任务流创建
	async.waterfall([

		//获取accessToken
		function(cb) {

			R.SEND_HTTP(
				req, {
					url: "http://"+config.publicConfig.SERVER_IP+":3400/weixin/getAccessToken",
//					url: "http://demo2.artmofang.com:3400/weixin/getAccessToken",
//					url: config.publicConfig.MIDDLE_API_SERVER + "weixin/getAccessToken",
					method: 'GET'
				},
				function(err, data) {

					if(err) {
						return RES.response(res, false, "获取accessToken失败");
					}

					access_token = data.access_token;
					//console.log("access_token:" + access_token);

					cb();

				}
			);

		},

		//设置临时二维码
		function(cb) {
			var params = {
				"expire_seconds": 604800,//有效时间
				"action_name": "QR_STR_SCENE",//二维码类型，QR_SCENE为临时的整型参数值，QR_STR_SCENE为临时的字符串参数值，QR_LIMIT_SCENE为永久的整型参数值，QR_LIMIT_STR_SCENE为永久的字符串参数值
				"action_info": {
					"scene": {
						"scene_str": newsParams || "test"
					}
				}
			}

			R.SEND_HTTP(
				req, {
					url: "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + access_token,
					method: 'POST',
					data: JSON.stringify(params)
				},
				function(err, data) {

					ticket = data.ticket;
					//console.log("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + ticket);
					resultData = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + ticket;

					cb();

				}
			);

		},

		//获取二维码
//		function(cb) {
//			R.SEND_HTTP(
//				req, {
//					url: "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + ticket,
//					method: 'GET'
//				},
//				function(err, data) {
//
//					//console.log("获取二维码:" + data);
//					resultData = data;
//
//					cb();
//
//				}
//			);
//
//		},

	], function(err, result) {
		//console.log('最终结果：')
		//console.log(resultData)
		//返回最终拼接结果
		RES.response(res, true, resultData);

	});

}